from turtle import *
import wx
from threading import Thread
from DebugTrace import DebugTrace
from time import sleep
class WxDrawing:
    def __init__(self,traceLevel=1,scalingFactor=1):
        self.curX=0
        self.curY=0
        self.curZ=0
        
        self.scaling=scalingFactor

        self.colorR=0
        self.colorG=0
        self.colorB=0

        self.trace = DebugTrace("Draw",traceLevel)
        self.lineSegments=[]

        Thread(target=self.initWxDraw).start()      

        #colormode(255)
        #pencolor(self.colorR,self.colorG,self.colorB)

        #screensize(7000,7000)
        #delay(1)
        #setworldcoordinates(0,0,600,600)

    def addLineSegment(self,segment):
        self.lineSegments.append(segment)
        self.dFrame.Refresh()
        self.trace.trace(3,"Line segment added to sim drawing")

        


    def initWxDraw(self):

        if not wx.PyApp.IsMainLoopRunning():
            self.drawApp=wx.App(False)

        if not wx.PyApp.IsMainLoopRunning():
            parent=None
        else:
            parent=app.GetTopWindow()
            print "Attaching to top window"
            
        self.dFrame=wx.Frame(parent,title="RepRap Simulation",size=(1000,1000))

        self.dPanel=wx.Panel(self.dFrame)
        self.dPanel.Bind(wx.EVT_PAINT,self.onPaint)
        self.dFrame.Show(True)
        self.dPanel.Show(True)
        self.dFrame.Centre()
        self.dFrame.Refresh()
        print "Window please!"

        
        if not wx.PyApp.IsMainLoopRunning():
            self.drawApp.MainLoop()
                
        


    def onPaint(self,event):
        dc=wx.PaintDC(event.GetEventObject())
        dc.Clear()
        dc.SetPen(wx.Pen("BLACK",1))
        #dc.DrawLine(0,0,50,50)
        print "Draw"
        dc.DrawLineList(self.lineSegments)

    def incrementColor(self):
        self.colorR+=50
        if self.colorR>255:
            self.colorR=0
            self.colorG+=50
            if self.colorG>255:
                self.colorG=0
                self.colorB+=50
                if self.colorB>255:
                    self.colorR=0
                    self.colorG=0
                    self.colorB=0
        self.trace.trace(3, "Pencolor now R:"+str(self.colorR)+" G:"+str(self.colorG)+" B:"+str(self.colorB))
        #pencolor(self.colorR,self.colorG,self.colorB)

    def checkZ(self,z):
        if z!=self.curZ:
            self.trace.trace(2,"Z now "+str(z))
            self.curZ=z
            self.incrementColor()

    def drawTo(self,x,y,z):
        self.trace.trace(2,"Drawing to X:"+str(x)+" Y:"+str(y)+" Z:"+str(z))
        self.checkZ(z)
        self.addLineSegment((self.curX,self.curY,x*self.scaling,y*self.scaling))
        self.moveTo(x,y,z)

    def moveTo(self,x,y,z):
        self.trace.trace(2,"Moving to X:"+str(x)+" Y:"+str(y)+" Z:"+str(z))
        self.curX=x*self.scaling
        self.curY=y*self.scaling
        self.curZ=z
